package com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_day.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.customerTwo.EMP.electricity_cost_records_day.entity.TElectricityCostRecordsDay;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_day.entity.TWaterCostRecordsDay;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_day.dto.TWaterCostRecordsDayListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_day.mapper.TWaterCostRecordsDayMapper;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_day.service.ITWaterCostRecordsDayService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 *  服务实现类
 *
 * @author dongxinhang
 * @since 2023-09-05
 */
@Service
@AllArgsConstructor
public class TWaterCostRecordsDayServiceImpl extends BaseService<TWaterCostRecordsDayMapper, TWaterCostRecordsDay> implements ITWaterCostRecordsDayService {


	@Override
	public IPage<TWaterCostRecordsDay> getPageList(TWaterCostRecordsDayListDto pageListDto) {
		//处理参数格式
		if(pageListDto.getYear().length()>4){
			pageListDto.setYear(pageListDto.getYear().substring(0,4));
		}
		if(pageListDto.getMonth().length()==1){
			pageListDto.setMonth("0"+pageListDto.getMonth());
		}
		//处理起始日期
		String start = pageListDto.getYear()+"-"+pageListDto.getMonth()+"-01";
		if(pageListDto.getMonth().length() == 1){
			start = pageListDto.getYear()+"-0"+pageListDto.getMonth()+"-01";
		}
		DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		LocalDate dateParam = LocalDate.parse(start, df);
		Wrapper<TWaterCostRecordsDay> wrapper = null;
		if(StringUtils.isNotBlank(pageListDto.getIdentifyId())){
			wrapper = Wrappers.<TWaterCostRecordsDay>query().lambda()
					.eq(!StringUtil.isEmpty(pageListDto.getIdentifyId()), TWaterCostRecordsDay::getIdentifyId, pageListDto.getIdentifyId())
					.eq(!StringUtil.isEmpty(pageListDto.getRoomNumber()), TWaterCostRecordsDay::getRoomNumber, pageListDto.getRoomNumber())
					.ge(!StringUtil.isEmpty(dateParam), TWaterCostRecordsDay::getDate, dateParam)
					.le(!StringUtil.isEmpty(dateParam.with(TemporalAdjusters.lastDayOfMonth())), TWaterCostRecordsDay::getDate,dateParam.with(TemporalAdjusters.lastDayOfMonth()));
		}else{
			wrapper = Wrappers.<TWaterCostRecordsDay>query().lambda()
					.isNull(TWaterCostRecordsDay::getIdentifyId)
					.eq(!StringUtil.isEmpty(pageListDto.getRoomNumber()), TWaterCostRecordsDay::getRoomNumber, pageListDto.getRoomNumber())
					.ge(!StringUtil.isEmpty(dateParam), TWaterCostRecordsDay::getDate, dateParam)
					.le(!StringUtil.isEmpty(dateParam.with(TemporalAdjusters.lastDayOfMonth())), TWaterCostRecordsDay::getDate,dateParam.with(TemporalAdjusters.lastDayOfMonth()));
		}
		return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public int addDayRecordsData(String start, String end, BigDecimal coldWaterPrice, BigDecimal heatWaterPrice) {
		return this.getBaseMapper().addDayRecordsData(start, end, coldWaterPrice, heatWaterPrice);
	}

	@Override
	public List<String> selectProportionIdList(String start, String end) {
		return this.getBaseMapper().selectProportionIdList(start, end);
	}

	@Override
	public int updateProportionData(List<String> ids) {
		return this.getBaseMapper().updateProportionData(ids);
	}

}